package com.wuzihao.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuzihao.reggie.common.R;
import com.wuzihao.reggie.entity.Employee;
import com.wuzihao.reggie.mapper.EmployeeMapper;
import com.wuzihao.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee>implements EmployeeService {
    @Autowired
    EmployeeService employeeService;

    @Override
    public R login(HttpServletRequest request, Employee employee) {
        //1.对密码进行加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据username查询数据库
        Employee employeeEntity = this.baseMapper.selectOne(new QueryWrapper<Employee>().eq("username", employee.getUsername()));
        //3.判断用户是否存在
        if(employee == null){
            return R.error("登录失败，用户不存在");
        }
        //4.判断密码是否正确
        String dbPassword = employeeEntity.getPassword();
        if(!dbPassword.equals(password)){
            return R.error("密码错误");
        }
        //5.判断登录状态是否被禁用
        if(employeeEntity.getStatus() == 0){
            return R.error("用户已被禁用");
        }
        //6.将员工id存入session
        request.getSession().setAttribute("employee",employeeEntity.getId());

        return R.success(employeeEntity);
    }

    @Override
    public void saveEmployee(HttpServletRequest request, Employee employee) {
        //设置初始密码123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        //获取当前用户id
        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(id);
//        employee.setCreateUser(id);
        employeeService.save(employee);
    }

    @Override
    public Page pageInfo(int page, int pageSize, String name) {
        //创建分页构造器
        Page pageInfo = new Page(page, pageSize);
        //创建条件构造器
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        //查询关键字
        //name:正式姓名 username:用户自定义
        queryWrapper.like(StringUtils.isNotEmpty(name),"name",name);
        queryWrapper.orderByDesc("update_time");

        employeeService.page(pageInfo,queryWrapper);
        return pageInfo;
    }

    @Override
    public void updateStatus(HttpServletRequest request, Employee employee) {
        Long id = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(id);
        employeeService.updateById(employee);
    }
}
